package com.mobartisan.workinglunch.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.igexin.sdk.PushManager;
import com.mobartisan.workinglunch.R;
import com.mobartisan.workinglunch.SnakeApplication;
import com.mobartisan.workinglunch.constants.Constants;
import com.mobartisan.workinglunch.getui_push.IntentPushRemindService;
import com.mobartisan.workinglunch.getui_push.PushRemindService;
import com.mobartisan.workinglunch.service.InternalExchangeService;
import com.mobartisan.workinglunch.ui.base.BaseActivity;
import com.mobartisan.workinglunch.util.ObjectUtils;
import com.mobartisan.workinglunch.util.SharedPreferencesUtil;

import java.io.File;


public class WelcomeActivity extends BaseActivity {

    private static final String TAG = WelcomeActivity.class.getSimpleName();

    private static final int REQUEST_PERMISSION = 0;
    String username, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //解决APP按home键时，再次点击桌面程序图标，程序重新启动的问题.
        if (!isTaskRoot()) {
            finish();
            return;
        }
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        window.setAttributes(params);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_welcome);
        username = SharedPreferencesUtil.getInstance().getStringValue("username");
        password = SharedPreferencesUtil.getInstance().getStringValue("password");

        PackageManager pkgManager = getPackageManager();

        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        boolean sdCardWritePermission =
                pkgManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

        // read phone state用于获取 imei 设备信息
        boolean phoneSatePermission =
                pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

        if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission) {

            requestPermission();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (ObjectUtils.isStringNotEmpty(username) && ObjectUtils.isStringNotEmpty(password)) {
                        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                        InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                        finish();
                    }
                }
            }, 2000);
            PushManager.getInstance().initialize(this.getApplicationContext(), PushRemindService.class);
        }

        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), IntentPushRemindService.class);

        // 应用未启动, 个推 service已经被唤醒,显示该时间段内离线消息
        if (SnakeApplication.payloadData != null) {
        }

//        // cpu 架构
//        Log.d(TAG, "cpu arch = " + (Build.VERSION.SDK_INT < 21 ? Build.CPU_ABI : Build.SUPPORTED_ABIS[0]));

        // 检查 so 是否存在
        File file = new File(this.getApplicationInfo().nativeLibraryDir + File.separator + "libgetuiext2.so");
        Log.e(TAG, "libgetuiext2.so exist = " + file.exists());
    }

    private void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE,Manifest.permission.INTERNET},
                REQUEST_PERMISSION);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_PERMISSION) {
            if ((grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
                PushManager.getInstance().initialize(this.getApplicationContext(), PushRemindService.class);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (ObjectUtils.isStringNotEmpty(username) && ObjectUtils.isStringNotEmpty(password)) {
                            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                            InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                            finish();
                        } else {
                            Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                            InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                            finish();
                        }
                    }
                }, 2000);
            } else {
                Log.e(TAG, "We highly recommend that you need to grant the special permissions before initializing the SDK, otherwise some "
                        + "functions will not work");
                PushManager.getInstance().initialize(this.getApplicationContext(), PushRemindService.class);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (ObjectUtils.isStringNotEmpty(username) && ObjectUtils.isStringNotEmpty(password)) {
                            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                            InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                            finish();
                        } else {
                            Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                            InternalExchangeService.startActivity(WelcomeActivity.this, intent);
                            finish();
                        }
                    }
                }, 2000);
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade, R.anim.hold);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
